package org.linlinjava.litemall.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.dto.AdminRewardDto;
import org.linlinjava.litemall.admin.service.AdminRewardService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 管理奖励控制器
 *
 * @author yliyun
 * @program: litemall
 * @description: 奖励规则配置
 * @author: chenyu
 * @create: 2022-03-15 16:34
 * @date 2022/09/14
 */
@Api(tags = "奖励规则API")
@RestController
@RequestMapping("/admin/reward")
@Validated
public class AdminRewardController {

    /**
     * 管理奖励服务
     */
    @Autowired
  private AdminRewardService adminRewardService;

    /**
     * 规则列表
     *
     * @param isExpired 是过期
     * @param ruleName  规则名称
     * @param status    状态
     * @param page      页面
     * @param limit     限制
     * @param sort      排序
     * @param order     订单
     * @return {@link Object}
     */
    @GetMapping("/listRule")
  @LogAnno
  public Object listRule(Byte isExpired,String ruleName,Byte status,@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit,
                         @Sort @RequestParam(defaultValue = "add_time") String sort,
                         @Order @RequestParam(defaultValue = "desc") String order){
    return adminRewardService.listRule(isExpired, page, limit, sort, order,ruleName,status);
  }

    /**
     * 添加
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PostMapping("/add")
  @LogAnno
  @Transactional
  public Object add(@RequestBody AdminRewardDto dto){
    return adminRewardService.add(dto,1);
  }

    /**
     * 更新
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PutMapping("/update")
  @LogAnno
  @Transactional
  public Object update(@RequestBody AdminRewardDto dto){
    return adminRewardService.update(dto);
  }

    /**
     * 删除
     *
     * @param ruleId 规则id
     * @return {@link Object}
     */
    @DeleteMapping("delete")
  @LogAnno
  public Object delete(@RequestParam Integer ruleId){
    return adminRewardService.delete(ruleId);
  }


    /**
     * 细节
     *
     * @param ruleId 规则id
     * @return {@link Object}
     */
    @GetMapping("/detail")
    @LogAnno
    @Transactional
    public Object detail(@RequestParam Integer ruleId){
    return adminRewardService.detail(ruleId);
  }

  /**
   * 期统计
   *
   * @param ruleId 规则id
   * @param page   页面
   * @param limit  限制
   * @return {@link ResponseUtil.Response}
   */
  @ApiOperation("统计周期")
    @GetMapping("periodStatistics")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ruleId",value = "规则ID",defaultValue = "0"),
            @ApiImplicitParam(name = "page",value = "page",defaultValue = "1"),
            @ApiImplicitParam(name = "limit",value = "limit",defaultValue = "20")
    })
    @LogAnno
    public ResponseUtil.Response periodStatistics(@RequestParam Integer ruleId,
                                                  @RequestParam Integer page,
                                                  @RequestParam Integer limit
                                                  ){
      return adminRewardService.periodStatistics(ruleId,page,limit);
  }



    /**
     * 更新状态
     *
     * @param ruleId 规则id
     * @return {@link Object}
     */
    @PutMapping("updateStatus")
    @LogAnno
    public Object updateStatus(@RequestParam Integer ruleId){
    return adminRewardService.updateStatus(ruleId);
  }

    /**
     * 日志
     *
     * @param registeDate registe日期
     * @param userName    用户名
     * @param email       电子邮件
     * @param page        页面
     * @param limit       限制
     * @param sort        排序
     * @param order       订单
     * @return {@link Object}
     */
    @GetMapping("log")
  @LogAnno
  public Object log(String registeDate,String userName,String email,
                    @RequestParam(defaultValue = "1") Integer page,
                    @RequestParam(defaultValue = "10") Integer limit,
                    @Sort @RequestParam(defaultValue = "add_time") String sort,
                    @Order @RequestParam(defaultValue = "desc") String order){
    return adminRewardService.log(registeDate,userName,email,page,limit);
  }

    /**
     * 详细日志
     *
     * @param logId 日志id
     * @return {@link Object}
     */
    @GetMapping("detailByLog")
  @LogAnno
  public Object detailByLog(Integer logId){
    return adminRewardService.detailByLog(logId);
  }

    /**
     * 奖励统计
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return {@link Object}
     */
    @GetMapping("rewardStatistic")
  @LogAnno
  public Object rewardStatistic(String startDate,String endDate){
    return adminRewardService.rewardStatistic(startDate,endDate);
  }

    /**
     * 分布统计
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return {@link Object}
     */
    @GetMapping("distributionStatistic")
  @LogAnno
  public Object distributionStatistic(String startDate,String endDate){
    return adminRewardService.distributionStatistic(startDate,endDate);
  }





}
